package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.product.dao.AttrAttrgroupRelationDao;
import com.mall.product.dto.AttrAttrgroupRelationDTO;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.vo.AttrGroupRelationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 属性&属性分组关联
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Service
public class AttrAttrgroupRelationServiceImpl extends CrudServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity, AttrAttrgroupRelationDTO> implements AttrAttrgroupRelationService {

    @Override
    public QueryWrapper<AttrAttrgroupRelationEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public AttrAttrgroupRelationEntity selectBYAttrId(Long attrId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public AttrAttrgroupRelationEntity findByAttrId(Long attrId) {
        return baseDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
    }

    @Override
    public void saveEntity(AttrAttrgroupRelationEntity relationEntity) {
        baseDao.insert(relationEntity);
    }

    @Override
    public void deleteBatchByParams(AttrGroupRelationVo[] relationVos) {

        List<AttrGroupRelationVo> collect = Arrays.stream(relationVos).peek(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(item.getAttrId());
            relationEntity.setAttrGroupId(item.getAttrGroupId());
        }).collect(Collectors.toList());
        if (collect.size()>0){
            baseDao.deleteByParams(collect);
        }
    }

    @Override
    public List<AttrAttrgroupRelationEntity> selectWithoutInCollection(List<Long> groupIds) {
        if (groupIds.size()>0)
            return baseDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().notIn("attr_group_id", groupIds));
        else
            return null;
    }

    @Override
    public List<AttrAttrgroupRelationEntity> selectWithInCollection(List<Long> groupIds) {
        if (groupIds.size()>0){
            return baseDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", groupIds));
        }
        return null;
    }

    @Override
    public List<AttrAttrgroupRelationEntity> listByAttrGroupId(List<Long> attrGroupIds) {
        return baseDao.listByAttrGroupId(attrGroupIds);
    }

    @Override
    public List<AttrAttrgroupRelationEntity> selectByAttrGroupId(Long attrGroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_group_id", attrGroupId);
        return baseDao.selectList(wrapper);
    }
}